package com.jj.net.kcp;

import com.jj.net.Channel;
import com.jj.net.Context;
import com.jj.net.Message;
import org.beykery.jkcp.KcpOnUdp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * Copyrights: QidianGroup@JJ
 *
 * @author: LuHan
 * @date: 2018/1/15, 10:55
 */
public class KcpContextImpl implements Context {
    private static Logger logger = LoggerFactory.getLogger(KcpContextImpl.class);

    private KcpChannel channel = null;
    private Map<String, Object> attrMap = new HashMap<>();


    public KcpContextImpl(KcpOnUdp kcpOnUdp){
        channel = new KcpChannel();
        channel.setKcpOnUdp(kcpOnUdp);
    }

    @Override
    public Object getAttr(String key) {
        return attrMap.get(key);
    }

    @Override
    public void setAttr(String key, Object value) {
        attrMap.put(key,value);
    }

    @Override
    public void close() {
        throw new RuntimeException("Not implement!");
    }

    @Override
    public Channel getChannel() {
        return channel;
    }

    @Override
    public void setChannel(Channel channel) {
        this.channel = (KcpChannel) channel;
    }

    @Override
    public void send(Message message) {
        logger.debug("prepare to send {}", message);
        if(channel != null){
            channel.send(message);
        }else{
            logger.error("endpoint null, ignore sending {}", message);
        }
    }

    public int getConv(){
        return channel.getKcpOnUdp().getKcp().getConv();
    }
}
